/*
 *
 *  Copyright (C) 2010 Tal Eisenberg
 *
 *  This program is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation, either version 3 of the License, or
 *  (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

package tal.drivechain.variable.types;

import tal.drivechain.matchers.ValueMatcher;
import java.util.ArrayList;
import java.util.List;
import tal.drivechain.chain.ChainNode;
import tal.drivechain.parsing.ParsingAid;
import tal.drivechain.parsing.Tokenizer;
import tal.drivechain.variable.Command;
import tal.drivechain.variable.Variable;
import tal.drivechain.variable.expressions.CustomExpression;

/**
 *
 * @author Main
 */
public class CommandType extends DataType {
    public CommandType() {
        super("command", Command.class, 12);
    }
    
    @Override
    public Object parseValue(String value) throws IllegalArgumentException {
        //name(param1,param2,param3)
        ArrayList<Variable> parameters = new ArrayList<Variable>();

        int openPar = ParsingAid.indexOfUnnested(value, '{', "([{'", ")]}'");
        if (openPar==-1)
            throw new IllegalArgumentException("Invalid syntax: " + value + " (A Command syntax is name{param1,param2,...} )");

        int closePar = Tokenizer.balanced(value, openPar, '{', '}');
        if (closePar==-1)
            throw new IllegalArgumentException("Invalid syntax: " + value + " (A Command syntax is name{param1,param2,...} )");

        String name = value.substring(0, openPar);
        Tokenizer parser = new Tokenizer(value.substring(openPar+1, closePar), ',');
        while (parser.hasMoreTokens()) {
            String token = parser.nextToken();
            if (token.length()!=0) {
                token = token.trim();
                parameters.add(Variable.parseVariable(name, null, token, false));
            }
        }
        
        return new Command(name, parameters);
    }

    @Override
    public ValueMatcher createValueMatcher() throws IllegalArgumentException {
        throw new IllegalArgumentException("Value matchers are not supported for command variables.");
    }

    @Override
    public Variable evaluateVar(Variable var, ChainNode context) {
        Command c = (Command)var.getInternalValue();
        List<Variable> params = c.getParameters();
        List<Variable> dest = new ArrayList<Variable>();
        for (Variable v : params) {
            
            Variable q = v.getDataType().evaluateVar(v, context);
            dest.add(q);
        } 
        Command destCommand = new Command(c.getName(), dest);
        return new Variable(var.getName(), DataType.COMMAND, destCommand, true);
    }

    @Override
    public String toString(Object value, ChainNode context) {
        return ((Command)value).toString(context);
    }

    @Override
    public CustomExpression createExpression(String value) throws IllegalArgumentException {
        throw new IllegalArgumentException("A command variable can't be assigned to an expression.");
    }

}
